package com.itdaan.portal.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author www.itdaan.com
 */
public class BaiduPostUtil {

	private static Logger logger = LoggerFactory.getLogger(BaiduPostUtil.class);
	
	private static SimpleDateFormat format = new SimpleDateFormat("yyyymmdd");
	private static String oldDate = "";
	private static boolean isstop = false;
	
	public static void main(String[] args) {
		for(int i = 0; i < 1000;i++)
		toBaidu("http://www.itdaan.com");
	}
	
	/**
	 * 推送
	 * @param itdaanurl
	 */
    public static void toBaidu(String itdaanurl) {
    	if(isstop){//如果目前处于暂停状态
    		String newDate = format.format(new Date());
    		if(newDate.equals(oldDate)){
    			isstop = false;
    			logger.info("主动推送开始");
    		}else{
    			logger.info("主动推送结束");
    			return;
    		}
    	}
        String url = "http://data.zz.baidu.com/urls?site=www.itdaan.com&token=CRivF41jEakT3gwq";
        String[] param = {itdaanurl};
        String json = Post(url, param);
        if(StringUtils.isEmpty(json)){
        	isstop = true;
        	oldDate = format.format(new Date());//如果当天推送完毕，则记录今天的日期
        }
        logger.info("推送结果："+json);
    }  
      
    /** 
     * 百度链接实时推送 
     * @param PostUrl 
     * @param Parameters 
     * @return 
     */  
    public static String Post(String PostUrl,String[] Parameters){  
        if(null == PostUrl || null == Parameters || Parameters.length ==0){  
            return null;  
        }  
        String result="";  
        PrintWriter out=null;  
        BufferedReader in=null;  
        try {  
            //建立URL之间的连接  
        	URL url = new URL(PostUrl);
            URLConnection conn=url.openConnection();  
            //设置通用的请求属性  
            conn.setRequestProperty("Host","data.zz.baidu.com");  
            conn.setRequestProperty("User-Agent", "curl/7.12.1");  
            conn.setRequestProperty("Content-Length", "83");  
            conn.setRequestProperty("Content-Type", "text/plain");  
               
            //发送POST请求必须设置如下两行  
            conn.setDoInput(true);  
            conn.setDoOutput(true);  
               
            //获取conn对应的输出流  
            out=new PrintWriter(conn.getOutputStream());  
            //发送请求参数  
            String param = "";  
            for(String s : Parameters){  
                param += s+"\n";  
            }  
            out.print(param.trim());  
            //进行输出流的缓冲  
            out.flush();
            //通过BufferedReader输入流来读取Url的响应  
            in=new BufferedReader(new InputStreamReader(conn.getInputStream()));  
            String line;  
            while((line=in.readLine())!= null){  
                result += line;  
            }  
        } catch (Exception e) {
//            e.printStackTrace();  
        } finally{  
            try{  
                if(out != null){  
                    out.close();  
                }  
                if(in!= null){  
                    in.close();  
                }  
                   
            }catch(IOException ex){  
                ex.printStackTrace();  
            }  
        }  
        return result;  
    }  
	
}
